<!-- Content Wrapper. Contains page content -->
<div class="box box-primary">
    <div class="box-header with-border">
        <h3 class="box-title">添加标签可以让亲们更快地找到你文章</h3>
    </div>
    <div>

        <div class="form-group">
            <label for="labelName">标签名</label>
            <input type="text" class="form-control" name="labelName" id="labelName" value="" placeholder="标签请不要太过于霸气!">
        </div>
        <input type="hidden" name="_method" value="get">

        <div class="form-group" id="labelDiv">
            {volist name="list" id="label"}
            <div class="btn"><span>{$label.name}</span>
                <button class="btn btn btn-warning" onclick="deleteLabel({$label.id},$(this))">X</button>
            </div>
            {/volist}
        </div>
        <div class="box-footer">
            <button type="submit" class="btn btn-primary" onclick="postLabel()">确认</button>
        </div>
    </div>

</div>
<script>
    $('#labelShow').addClass('active');
    function postLabel() {
        var ob = $('#labelName');
        var name = ob.val();
        if (name == '') {
            alert('不写内容提交个P');
            return false;
        } else {
            if (name.length <= 10) {
                $.post(
                        "{:url('article/saveLabel')}",
                        {name: name},
                        function (data) {
                            if (data.status) {
                                ob.val(null);
                                $('#labelDiv').append('<div class="btn" > <span>' + data.name + '</span> <button class="btn btn btn-warning" onclick="deleteLabel(' + data.id + ')">X</button> </div>');
                            } else {
                                alert(data.info);
                            }
                        });
            } else {
                alert('写太长了,请控制在10个汉字以内');
            }
        }
    }
    function deleteLabel(id, ob) {
        if (confirm('是否删除标签?')) {
            $.get(
                    "{:url('article/deleteLabel')}",
                    {id: id},
                    function (data) {
                        if (data.status) {
                            ob.parent().remove()
                        } else {
                            alert(data.info);
                        }
                    }
            );
        } else {
            return false;
        }
    }

</script>